package com.leyou.item.web;

import com.leyou.common.dto.BrandDTO;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    @GetMapping("/list")//根据品牌id集合查询品牌集合
    public ResponseEntity<List<Brand>> queryBrandsByids(@RequestParam("ids") List<Long> ids) {
        List<Brand> brands = brandService.queryBrandsByids(ids);
        return ResponseEntity.ok(brands);


    }
    @GetMapping("/page")//分页查询品牌
    public ResponseEntity<PageDTO<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "key",required = false) String key
    ){
        return ResponseEntity.ok(this.brandService.queryBrandByPage(page,rows,key));

    }

    //根据分类id查询品牌
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> listBrandById(@RequestParam("id") Long id){
        return ResponseEntity.ok(this.brandService.listBrandById(id));
    }

    //根据id查询品牌
    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(new BrandDTO(this.brandService.getById(id)));
    }

    //新增品牌
    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO) {
        brandService.saveBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
}
